﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtendMethod
{
    class TestSysExtendMethod
    {
        public void Test()
        {
            List<Person> pList = new List<Person>()
            {
                new Person{IDno=10000,Name="王老师",Gender="男",Address="天津"},
                new Person{IDno=10020,Name="张老师",Gender="女",Address="河北"},
                new Person{IDno=10030,Name="刘老师",Gender="男",Address="江西"},
                new Person{IDno=10011,Name="常老师",Gender="女",Address="北京"},
                new Person{IDno=10019,Name="胡老师",Gender="女",Address="上海"},
            };
            //实际中，List中存储的数据采集对象，可能会非常多...

            //问题:如何从List中快速找到需要的对象呢?
            //初学方法；使用遍历的方法
            List<Person> pResult = new List<Person>();

            foreach (var item in pList)
            {
                if(item.IDno > 10020 && item.Address == "江西")
                {
                    pResult.Add(item);
                }
            }
            foreach (var item in pResult)
            {
                Console.WriteLine(item.IDno + " "+item.Address + " "+item.Name);
            }
            Console.WriteLine("--------------------------------------");

            //使用扩展方法
            var result = pList.Where(p => p.IDno > 10020 && p.Address == "江西");
            foreach (var item in result)
            {
                Console.WriteLine(item.IDno + " " + item.Address + " " + item.Name);
            }
            var result1 = pList.OrderBy(p => p.Name);
            var result2 = pList.OrderByDescending(p => p.IDno);


        }
    }
    
    class Person
    {
        public int IDno { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string Address { get; set; }
    }
}
